package cn.com.xf.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.xf.common.AbstractBaseController;
import cn.com.xf.common.Constants;
import cn.com.xf.common.StringUtil;
import cn.com.xf.domain.TblCheckCmd;
import cn.com.xf.domain.TblCheckDevice;
import cn.com.xf.domain.TblCheckFp;
import cn.com.xf.domain.TblCheckUser;
import cn.com.xf.service.CheckUserService;
import cn.com.xf.service.SettingService;
import cn.com.xf.utils.encode.EncrypTool;
import cn.com.xf.web.PageModel;
import cn.com.xf.web.form.SettingForm;

@Controller
@RequestMapping("/check")
public class CheckDeviceController extends AbstractBaseController {

	@Autowired
	private SettingService settingService;

	@Autowired
	private CheckUserService checkUserService;

	@RequestMapping(value = "/addEmployee")
	public ModelAndView addEmployee(HttpServletRequest request, HttpServletResponse response, SettingForm form)
			throws Exception {
		String siteid = getDefaultSiteid();
		List<TblCheckDevice> list = settingService.selectDevice(getSiteList());
		ModelAndView mv = new ModelAndView("/check/employee_add", Constants.FORM_COMMAND_KEY, form);
		mv.addObject("defaultSiteid", siteid);
		mv.addObject("devices", list);
		return mv;
	}

	@RequestMapping(value = "/doAddEmployee")
	@ResponseBody
	public Map<String, Object> doAddEmployee(HttpServletRequest request, HttpServletResponse response, SettingForm form)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(form.getUserId()) && StringUtils.isNotBlank(form.getSn())) {
			String[] userids = form.getUserId().split(",");
			List<TblCheckCmd> cmds = new ArrayList<TblCheckCmd>();
			List<TblCheckUser> users = new ArrayList<TblCheckUser>();
			for (String userid : userids) {
				String[] parts = userid.split("_");
				TblCheckCmd cmd = new TblCheckCmd();
				cmd.setCmd("DATA USER PIN=" + parts[0] + "\tName=" + parts[1]);
				cmd.setSn(form.getSn());
				cmd.setStatus((short) 0);
				cmds.add(cmd);
				List<TblCheckFp> fps = checkUserService.selectFp(parts[0]);
				for (TblCheckFp fp : fps) {
					TblCheckCmd c = new TblCheckCmd();
					c.setCmd("DATA UPDATE FINGERTMP PIN=" + parts[0] + "\tValid=1\tSize=" + fp.getLength() + "\tFID="
							+ fp.getFid() + "\tTMP=" + fp.getTmp());
					c.setSn(form.getSn());
					c.setStatus((short) 0);
					cmds.add(c);
				}
				TblCheckUser user = checkUserService.selectUser(form.getSn(), parts[0]);
				if (user == null) {
					user = new TblCheckUser();
					user.setSn(form.getSn());
					user.setUserid(parts[0]);
					user.setUsername(parts[1]);
					user.setFlag((short) 0);
					user.setStatus((short) 1);
					users.add(user);
				}
			}
			settingService.insertCmds(cmds);
			checkUserService.insertBatch(users);
			map.put("status", "SUCCESS");
			return map;
		}
		map.put("msg", "请填写字段完整");
		map.put("status", "FAILD");
		return map;
	}

	@RequestMapping("/employeeList")
	public ModelAndView employeeList(HttpServletRequest request, HttpServletResponse response, SettingForm form)
			throws Exception {
		List<TblCheckDevice> devices = settingService.selectDevice(getSiteList());
		ModelAndView mv = new ModelAndView("/check/employee_list", Constants.FORM_COMMAND_KEY, form);
		String sn = StringUtil.StringFilter(form.getSn());
		String userid = StringUtil.StringFilter(form.getUserId());
		if (sn.equals("")) {
			if (devices.size() > 0) {
				sn = devices.get(0).getSn();
			} else
				return mv;
		}
		form.setSn(sn);
		form.setUserId(userid);
		int count = checkUserService.selectUserCount(sn, userid);
		if (count == 0)
			return mv;

		int pageNum = 1;
		if (StringUtils.isNotBlank(form.getPageNum())) {
			pageNum = Integer.valueOf(form.getPageNum());
		}
		PageModel pageModel = new PageModel(count, PageModel.DEFAULT_PAGE_SIZE, pageNum, form);
		int begin = pageModel.getBegin();
		int pageSize = pageModel.getPageSize();
		List<TblCheckUser> list = checkUserService.selectUsers(begin, pageSize, sn, userid);
		mv.addObject("list", list);
		return mv;
	}

	@RequestMapping("/deleteEmployee")
	@ResponseBody
	public Map<String, Object> deleteEmployee(HttpServletRequest request, HttpServletResponse response,
			SettingForm form) throws Exception {
		String idStr = form.getId();
		String realId = "";
		if (StringUtils.isNotBlank(idStr)) {
			realId = EncrypTool.dencode(idStr);
		}
		TblCheckUser user = checkUserService.selectUser(new Long(realId));
		TblCheckCmd cmd = new TblCheckCmd();
		cmd.setSn(user.getSn());
		cmd.setStatus((short) 0);
		cmd.setCmd("DATA DEL_USER PIN=" + user.getUserid());
		settingService.insertCmd(cmd);
		Map<String, Object> map = new HashMap<String, Object>();
		checkUserService.deleteUser(new Long(realId));
		map.put("status", "SUCCESS");
		return map;
	}

	@RequestMapping("/devices")
	@ResponseBody
	public List<TblCheckDevice> getDevices() {
		List<TblCheckDevice> list = settingService.selectDevice(getSiteList());
		return list;
	}
}
